﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：ValidGB2312LengthAttribute.cs
// 功能描述：TongTongMall.MallWeb.Application
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TongTongMall.MallWeb.Application.Attributes.ModelBinders
{
    public class ValidGB2312LengthAttribute : StringLengthAttribute
    {

        public ValidGB2312LengthAttribute(int maximumLength) : base(maximumLength)
        {

        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var result = ValidationResult.Success;
            if (value != null)
            {
                var length = Encoding.GetEncoding("gb2312").GetByteCount(value.ToString());
                if (length > MaximumLength)
                {
                    return new ValidationResult(base.ErrorMessage);
                }
            }
            return result;
        }
    }
}
